﻿using Abp.Application.Services.Dto;
using Abp.Web.Models;
using Abp.Web.Mvc.Authorization;
using KDS.Mes.Mes.Factorys;
using KDS.Mes.Mes.Factorys.Dtos;
using KDS.Mes.Web.Models.Factorys;
using KDS.Mes.Web.Controllers;
using System;
using System.Threading.Tasks;
using System.Web.Mvc;
using KDS.Mes.Authorization;


namespace KDS.Mes.Web.Controllers
{
    public class FactorysController : MesControllerBase
    {

        private readonly IFactoryAppService _factoryAppService;

        public FactorysController(IFactoryAppService factoryAppService)
        {
            _factoryAppService = factoryAppService;
           
        }

        public  ActionResult Index()
        {
		   var model = new GetFactoryInput {FilterText = Request.QueryString["filterText"]};
            return View(model);
        }

        [DontWrapResult]
        public JsonResult GetAllFactorys(int limit, int offset, string sortfiled, string sortway)
        {
            var sort = !string.IsNullOrEmpty(sortfiled) ? string.Format("{0} {1}", sortfiled, sortway) : "";
            var filter = new GetFactoryInput
            {
                SkipCount = offset,
                MaxResultCount = limit,
                Sorting = sort,
            };
            var pagedTasks = _factoryAppService.GetPagedFctorys(filter);
            var j = Json(new { total = pagedTasks.TotalCount, rows = pagedTasks.Items }, JsonRequestBehavior.AllowGet);
            return Json(new { total = pagedTasks.TotalCount, rows = pagedTasks.Items }, JsonRequestBehavior.AllowGet);
        }




        /// <summary>
        /// 根据id获取进行编辑或者添加的用户信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [AbpMvcAuthorize(AppPermissions.Pages_Basic_Factorys_Create, AppPermissions.Pages_Basic_Factorys_Edit)]
        public async Task<PartialViewResult> CreateOrEditFactoryModal(int? id)
        {
		var input=new NullableIdDto<int>{Id=id};
	 
                 var output=    await _factoryAppService.GetFactoryForEditAsync(input);

				 var viewModel=new CreateOrEditFactoryModalViewModel(output);


            return PartialView("_CreateOrEditFactoryModal",viewModel);
        }
	 
       
    }
}